package jdly.water.api.service.base;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jdly.water.api.common.service.R;
import jdly.water.api.dao.report.IndexDao;
import jdly.water.api.util.NumberUtil;

@Service
public class IndexService {

	@Autowired
	private IndexDao indexDao;

	public R index() {
		R result = R.ok();
		try {
			
			List<BigDecimal> datalist = indexDao.baseInfo();
			Map<String, String> r = new HashMap<String, String>();
			String usercount = datalist.get(0).toString();
			String bmcount = datalist.get(1).toString();
			String bmtcount = datalist.get(2).toString();
			String ccount = datalist.get(3).toString();
			r.put("usercount", usercount);
			r.put("bmcount", bmcount);
			r.put("bmtcount", bmtcount);
			r.put("ccount", ccount);
			uploadInfo(r);
			cost(r);
			result.put("indexdata", r);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return result;
	}

	public void uploadInfo(Map<String, String> r) {
		List<BigDecimal> datalist = indexDao.uploadInfo();
		String ccount = datalist.get(0).toString();
		String usercount = datalist.get(1).toString();
		String bmcount = datalist.get(2).toString();
		String bmtcount = datalist.get(3).toString();
		r.put("uusercount", usercount);
		r.put("ubmcount", bmcount);
		r.put("ubmtcount", bmtcount);
		r.put("uccount", ccount);
	}

	private void cost(Map<String, String> r) {
		List<BigDecimal> datalist = indexDao.cost();
		String cost = datalist.get(0).toString();
		String balance = datalist.get(1).toString();
		String flow = NumberUtil.format2DecimalStr(datalist.get(2).doubleValue());
		r.put("cost", cost);
		r.put("balance", balance);
		r.put("flow", flow);
	}
}
